package com.sdyeda.readiot.fx;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.alibaba.fastjson.JSONObject;
import com.sdyeda.readiot.ReadDtuTaskService;
import com.sdyeda.readiot.ReadTaskService;
import com.sdyeda.readiot.config.Setting;
import com.sdyeda.readiot.iotinfo.IotData;
import com.sdyeda.readiot.iotinfo.IotInfo;
import com.sdyeda.readiot.iotinfo.IotSavedData;
import com.sdyeda.readiot.iotinfo.JdbcIotInfo;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;

public class DetailWindowController {

	private IotData iotdata;
	
	private Stage diagstage;
	
	private ReadDialogController readdialogcontroller;

	@FXML private TableColumn<IotSavedData, Date> coltime;
	@FXML private TableView<IotProperty> tvlist;
	@FXML private TableView<IotSavedData> tvsavedlist; 
	@FXML private Button btnreset;
	
	@FXML public void initialize() {
		/*
		if(!"018".contains(iotdata.getType())) {
			btnreset.setDisable(true);
		}
		*/ 

		// 时间——格式化时间
		
		coltime.setCellFactory(c -> new TableCell<IotSavedData, Date>() {
			@Override
			protected void updateItem(Date value, boolean empty) {

				super.updateItem(value, empty);
				if (empty || value == null) {
					setText(null);
				} else {
					setAlignment(Pos.CENTER);
					setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA).format(value));
				}
			}
		});

		
	}
	
	@FXML void readData(){
		List<IotSavedData> data = JdbcIotInfo.readSavedDate(iotdata);
		if (data!=null) {
			tvsavedlist.setItems(FXCollections.observableArrayList(data));
			tvsavedlist.refresh();
		}
	}
	
	@FXML void resetAction() {
		readdialogcontroller.setDetailwindowcontroller(this);

		if(!"018".contains(iotdata.getType())) {
			new Alert(AlertType.ERROR,"不支持该类型设备").show();
			return;
		}
			
		TextInputDialog dialog = new TextInputDialog();
		dialog.setContentText("密码");
		dialog.setHeaderText("请输入密码");
		dialog.setTitle("请输入密码");
		dialog.showAndWait();
		String result = dialog.getResult();

		//System.out.println(result);
		
		if(!Setting.Get(Setting.SET_PASSWORD).equals(result)){
			new Alert(AlertType.ERROR,"密码错误").show();
			return;
		}
		
		readdialogcontroller.setDetailwindowcontroller(this);

		String command = "reset";
		getDiagstage().show();
		
		IotInfo iotinfo = setIotInfo();
		if (iotinfo.isUsedtu()) {
			Thread reset = new Thread(() -> {
				ReadDtuTaskService readtask = new ReadDtuTaskService();
				readtask.ReadComStepByStep(iotinfo, command, readdialogcontroller);
			});
			reset.start();// ddd
		} else {
			Thread reset = new Thread(() -> {
				ReadTaskService readtask = new ReadTaskService();
				readtask.ReadComStepByStep(iotinfo, command, readdialogcontroller);
			});
			reset.start();
		}

	}
	
	@FXML public void readAction() {
		readdialogcontroller.setDetailwindowcontroller(this);

		getDiagstage().show();
		
		IotInfo iotinfo = setIotInfo();

		String command ="read";

		if (iotinfo.isUsedtu()) {
			Thread read = new Thread(() -> {
				ReadDtuTaskService readtask = new ReadDtuTaskService();

				//if (iotinfo.getType().equals("5")) {
					//command = 0x1A; // 雨量计读取今天数据
				//}
				readtask.ReadComStepByStep(iotinfo, command, readdialogcontroller);
			});
			read.start();
			// ddd
		} else {
			Thread read = new Thread(() -> {
				ReadTaskService readtask = new ReadTaskService();
				//if (iotinfo.getType().equals("5")) {
					//command = "read"; // 雨量计读取今天数据
				//}
				readtask.ReadComStepByStep(iotinfo, command, readdialogcontroller);
			});
			read.start();
		}
	}
	
	IotInfo setIotInfo() {
		IotInfo iotinfo = new IotInfo();
		if(iotdata.getCom().contains("(") && iotdata.getCom().contains(")"))	//dtu
		{
			iotinfo.setIp(iotdata.getCom().substring(0,iotdata.getCom().indexOf("(")));
			//System.out.println(iotdata.getCom().substring(0,iotdata.getCom().indexOf("(")));
			//System.out.println(iotdata.getCom().substring(iotdata.getCom().indexOf("(")+1,iotdata.getCom().indexOf(")")));
			try {
				iotinfo.setPort(Integer.parseInt(iotdata.getCom().substring(iotdata.getCom().indexOf("(")+1,iotdata.getCom().indexOf(")"))));
			}
			catch(Exception e) {
				System.out.println(false);
			}
			
			iotinfo.setUsedtu(true);
		}
		else {
			iotinfo.setCom(iotdata.getCom());
		}
		iotinfo.setIotid(iotdata.getIotid());
		iotinfo.setType(iotdata.getType());
		return iotinfo;
	}

	public Stage getDiagstage() {
		return diagstage;
	}

	public void setDiagstage(Stage diagstage) {
		this.diagstage = diagstage;
	}

	public IotData getIotdata() {
		return iotdata;
	}

	public void setIotdata(IotData iotdata) {
		this.iotdata = iotdata;

		refushTvList();
	}
	

	public void refushTvList(IotData iotdata) {
		setIotdata(iotdata);
		refushTvList();
	}
	
	void refushTvList() {
		List<IotProperty> iotproperty = new ArrayList<>();
		iotproperty.add(new IotProperty("类型", iotdata.getTypetext()));
		iotproperty.add(new IotProperty("编号", iotdata.getId()));
		iotproperty.add(new IotProperty("设备编号", iotdata.getIotid()));
		iotproperty.add(new IotProperty("端口", iotdata.getCom()));
		if(iotdata.getType().equals("5")) {	//雨量计
			iotproperty.add(new IotProperty("小时雨量", String.valueOf(iotdata.getValue())));
			iotproperty.add(new IotProperty("日雨量", String.valueOf(iotdata.getOffset())));
			iotproperty.add(new IotProperty("每小时雨量", JSONObject.toJSONString(iotdata.getRainvalues())));
		}
		else {
			iotproperty.add(new IotProperty("测量值", String.valueOf(iotdata.getValue())));
			iotproperty.add(new IotProperty("偏差值", String.valueOf(iotdata.getOffset())));
		}
		iotproperty.add(new IotProperty("测量单位", String.valueOf(iotdata.getUnit())));
		iotproperty.add(new IotProperty("设备型号", String.valueOf(iotdata.getStype())));
		iotproperty.add(new IotProperty("温度值", String.valueOf(iotdata.getTemp())));
		iotproperty.add(new IotProperty("应变频率", String.valueOf(iotdata.getStrain())));
		iotproperty.add(new IotProperty("补偿频率", String.valueOf(iotdata.getCompen())));
		String date = "" ;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(iotdata.getTime());
		}catch(Exception e) {
			
		}
		iotproperty.add(new IotProperty("测量时间", date));
		iotproperty.add(new IotProperty("读取状态", (iotdata.isSuccess()?"成功":"失败")));

		//new IotProperty("编号", iotdata.getId().toString()));
		tvlist.setItems(FXCollections.observableArrayList(iotproperty));
		tvlist.refresh();
	}
	



	public ReadDialogController getReaddialogcontroller() {
		return readdialogcontroller;
	}

	public void setReaddialogcontroller(ReadDialogController readdialogcontroller) {
		this.readdialogcontroller = readdialogcontroller;
	}




	public class IotProperty{
		private StringProperty property;
		private StringProperty value;
		
		public IotProperty(String property,String value) {
			setProperty(property);
			setValue(value);
		}
		
		public String getProperty() {
			return propertyProperty().get();
		}
		public void setProperty(String value) {
			propertyProperty().set(value);
		}
		
		 public StringProperty propertyProperty() {
	         if (property == null) property = new SimpleStringProperty(this, "property");
	         return property;
	     }
		/**
		 * @return the value
		 */
		public String getValue() {
			return valueProperty().get();
		}
		/**
		 * @param value the value to set
		 */
		public void setValue(String value) {
			valueProperty().set(value);
		}
		
		 public StringProperty valueProperty() {
	         if (value == null) value = new SimpleStringProperty(this, "value");
	         return value;
	     }
		
	}




	
	

	 

}
